<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * recommend_model
 *  推荐游戏管理模型
 */
class Recommend_model extends CI_Model
{
    var $_table_name = 'recommend';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $page 分页开始数
     * @param $offset  分页数量
     * @param array $view 搜索关键词
     * @return array
     */
    public function get_list($page, $offset, $view)
    {
        $this->db->select('r.*,c.title as category_title');
        $this->db->from("$this->_table_name r");
        $this->db->join('category as c', 'c.id=r.category_id', 'left outer');
        if (!empty($view['title'])) {
            $this->db->like('r.title', $view['title']);
        }
        if (!empty($view['category_id'])) {
            $this->db->where('r.category_id', $view['category_id']);
        }
        if (!empty($view['type'])) {
            $this->db->where('r.type', $view['type']);
        }
        if (!empty($view['breakid'])) {
            $this->db->where('r.breakid', $view['breakid']);
        }

        $this->db->order_by("r.sort desc,r.click desc,r.createtime desc");
        $this->db->limit($offset, $page);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * 获取总量
     * @param array $view 搜索关键词
     * @return array
     */
    public function total_rows($view)
    {
        $this->db->select('count(*) as num');
        $this->db->from("$this->_table_name");

        if (!empty($view['title'])) {
            $this->db->like('title', $view['title']);
        }
        if (!empty($view['category_id'])) {
            $this->db->where('category_id', $view['category_id']);
        }
        if (!empty($view['type'])) {
            $this->db->where('type', $view['type']);
        }
        if (!empty($view['breakid'])) {
            $this->db->where('breakid', $view['breakid']);
        }

        $query = $this->db->get();
        $row = $query->row_array();
        return $row['num'];
    }

    public function images_list($id)
    {
        $this->db->select('*');
        $this->db->from("recommend_picture");
        $this->db->where('recommend_id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * 获取相关有奖游戏
     */
    public function category_list($id, $category_id, $type)
    {
        $sql = "select * from 17koko_recommend where category_id={$category_id} and type={$type} and id != {$id} order by sort desc , id desc limit 0 ,4";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function get_one($id)
    {
        $this->db->select('r.*,c.title as category_title');
        $this->db->from("$this->_table_name r");
        $this->db->join('category as c', 'c.id=r.category_id', 'left outer');
        $this->db->where('r.id', $id);
        $query = $this->db->get();
        return $query->row_array();
    }

}